package com.great.medical.goods.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.great.medical.common.ResultBody;
import com.great.medical.common.StatusEnum;
import com.great.medical.goods.db.model.GoodsItem;
import com.great.medical.goods.db.model.OriginGoods;
import com.great.medical.goods.db.model.ProtocolGoods;
import com.great.medical.goods.dto.GoodsItemAddRequest;
import com.great.medical.goods.dto.GoodsItemUpdateRequest;
import com.great.medical.goods.service.GoodsItemService;
import com.great.medical.goods.service.OriginGoodsService;
import com.great.medical.goods.service.ProtocolGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;


@RestController
@RequestMapping("/protocolGoods")
@Slf4j
@SuppressWarnings("unchecked")
public class ProtocolGoodsController {

    @Autowired
    private ProtocolGoodsService protocolGoodsService;

    @Autowired
    private OriginGoodsService originGoodsService;

    @Autowired
    private GoodsItemService goodsItemService;

    @GetMapping("/getPageList")
    public ResultBody<IPage<ProtocolGoods>> getPageList(@RequestParam(required = false) Map map) {
        return ResultBody.ok().data(protocolGoodsService.getPageList(map));
    }

    @GetMapping("/list")
    public ResultBody<List<ProtocolGoods>> list(@RequestParam Long classifyId) {
        final List<ProtocolGoods> list = protocolGoodsService.lambdaQuery()
                .eq(ProtocolGoods::getClassifyId, classifyId)
                .eq(ProtocolGoods::getStatus, StatusEnum.ENABLE.getCode()).list();
        return ResultBody.ok().data(list);
    }


    @PostMapping("/save")
    public ResultBody<Void> save(@RequestBody @Valid  GoodsItemAddRequest request) {
        final Long originGoodsId = request.getOriginGoodsId();
        final OriginGoods originGoods = originGoodsService.getById(originGoodsId);
        if (Objects.isNull(originGoods)) {
            return ResultBody.failed().msg("供给商品不存在");
        }
        ProtocolGoods protocolGoods = protocolGoodsService.lambdaQuery().eq(ProtocolGoods::getOriginGoodsId, originGoods.getId())
                .eq(ProtocolGoods::getConfiguration, request.getConfiguration())
                .one();
        if (!Objects.isNull(protocolGoods)) {
            return ResultBody.failed().msg("该商品配置已存在,请勿重复添加");
        } else {
            protocolGoods = new ProtocolGoods();
            protocolGoods.setClassifyId(originGoods.getClassifyId());
            protocolGoods.setOriginGoodsId(originGoods.getId());
            protocolGoods.setOriginGoodsName(originGoods.getName());
            protocolGoods.setConfiguration(request.getConfiguration());
            protocolGoods.setStatus(StatusEnum.ENABLE.getCode());
            protocolGoods.setCreateTime(new Date());
            protocolGoodsService.save(protocolGoods);
        }
        //如果之前有添加过想过的规格商品信息,先删除
        final List<GoodsItem> goodsItemList = goodsItemService.lambdaQuery()
                .eq(GoodsItem::getProtocolGoodsId, protocolGoods.getId())
                .list();
        if (CollectionUtil.isNotEmpty(goodsItemList)) {
            goodsItemService.removeByIds(goodsItemList);
        }
        final List<GoodsItem> itemsList = new ArrayList<>();
        for (GoodsItemAddRequest.Items items : request.getItemsList()) {
            GoodsItem goodsItem = GoodsItem.builder()
                    .protocolGoodsId(protocolGoods.getId())
                    .color(items.getColor())
                    .price(items.getPrice())
                    .upDownPrice(items.getUpDownPrice())
                    .supplyDemand(items.getSupplyDemand())
                    .status(items.getStatus())
                    .createTime(new Date())
                    .build();
            itemsList.add(goodsItem);
        }
        goodsItemService.saveBatch(itemsList);
        return ResultBody.ok();
    }


    @PostMapping("/update")
    public ResultBody<Void> update(@RequestBody @Valid  GoodsItemUpdateRequest request) {
        final Long protocolGoodsId = request.getProtocolGoodsId();
        final ProtocolGoods protocolGoods = protocolGoodsService.getById(protocolGoodsId);
        if (Objects.isNull(protocolGoods)) {
            return ResultBody.failed().msg("协议商品不存在");
        }
        if (!Objects.isNull(request.getStatus())) {
            protocolGoods.setStatus(request.getStatus());
            protocolGoodsService.updateById(protocolGoods);
        }

        //如果之前有添加过想过的规格商品信息,先删除
        final List<GoodsItem> goodsItemList = goodsItemService.lambdaQuery()
                .eq(GoodsItem::getProtocolGoodsId, protocolGoods.getId())
                .list();
        if (CollectionUtil.isNotEmpty(goodsItemList)) {
            goodsItemService.removeByIds(goodsItemList);
        }
        final List<GoodsItem> itemsList = new ArrayList<>();
        for (GoodsItemUpdateRequest.Items items : request.getItemsList()) {
            GoodsItem goodsItem = GoodsItem.builder()
                    .protocolGoodsId(protocolGoods.getId())
                    .color(items.getColor())
                    .price(items.getPrice())
                    .upDownPrice(items.getUpDownPrice())
                    .supplyDemand(items.getSupplyDemand())
                    .status(items.getStatus())
                    .createTime(new Date())
                    .build();
            itemsList.add(goodsItem);
        }
        goodsItemService.saveBatch(itemsList);
        return ResultBody.ok();
    }


    @GetMapping("/detail")
    public ResultBody<ProtocolGoods> detail(@RequestParam Long protocolGoodsId) {
        ProtocolGoods protocolGoods = protocolGoodsService.getById(protocolGoodsId);
        if (Objects.isNull(protocolGoods)) {
            return ResultBody.failed().msg("协议商品不存在");
        }
        final List<GoodsItem> goodsItemList = goodsItemService.lambdaQuery().eq(GoodsItem::getProtocolGoodsId, protocolGoodsId).list();
        protocolGoods.setItemList(goodsItemList);
        return ResultBody.ok().data(protocolGoods);
    }


    @GetMapping("/findByOriginGoodsId")
    public ResultBody<List<ProtocolGoods>> findByOriginGoodsId(@RequestParam Long originGoodsId) {
        final List<ProtocolGoods> list = protocolGoodsService.lambdaQuery().eq(ProtocolGoods::getOriginGoodsId, originGoodsId)
                .list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (ProtocolGoods protocolGoods : list) {
                final List<GoodsItem> goodsItemList = goodsItemService.lambdaQuery().eq(GoodsItem::getProtocolGoodsId, protocolGoods.getId()).list();
                protocolGoods.setItemList(goodsItemList);
            }
        }
        return ResultBody.ok().data(list);
    }

}
